package com.ks.user.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ks.core.base.OperationContext;
import com.ks.core.constant.Const;
import com.ks.core.exception.BizExceptionEnum;
import com.ks.core.exception.GunsException;
import com.ks.core.util.ToolUtil;
import com.ks.user.dao.ApBaseMapper;
import com.ks.user.entity.ApBase;
import com.ks.user.entity.ApTenantAllow;
import com.ks.user.dao.ApTenantAllowMapper;
import com.ks.user.service.IApTenantAllowService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 应用-租户授权对照表 服务实现类
 * </p>
 *
 * @author kingdee123
 * @since 2020-12-11
 */
@Service
public class ApTenantAllowServiceImpl extends ServiceImpl<ApTenantAllowMapper, ApTenantAllow> implements IApTenantAllowService {
    @Resource
    private ApTenantAllowMapper apTenantAllowMapper;

    @Resource
    private ApBaseMapper ApBaseMapper;

    @Override
    public int appEmpower(OperationContext context, List<ApBase> apBaseList) {
        int quantity = 0;
        for (ApBase apBase : apBaseList) {
            EntityWrapper<ApTenantAllow> wrapper = new EntityWrapper<>();
            ApTenantAllow selectAptent = new ApTenantAllow();
            selectAptent.setTenantID(apBase.getTenantID());
            selectAptent.setApID(apBase.getId());
            selectAptent.setCAllowType(Const.OrgBase.AUTHORIZATION_METHOD_L1);
            wrapper.setEntity(selectAptent);
            List<ApTenantAllow> apTenantAllowList = apTenantAllowMapper.selectList(wrapper);

            if (apTenantAllowList.size() > 0) {
                selectAptent = apTenantAllowList.get(0);
                selectAptent.setDelMark(Const.DEL_FLAG_NORMAL);
                selectAptent.setUpdateUserID(context.getUserId().intValue());
                Integer i = apTenantAllowMapper.updateById(selectAptent);
                quantity += i;
            } else {
                ApTenantAllow apTenantAllow = new ApTenantAllow();
                apTenantAllow.setApID(apBase.getId());
                apTenantAllow.setTenantID(apBase.getTenantID());
                selectAptent.setCreateUserID(context.getUserId().intValue());
                Integer i = apTenantAllowMapper.insert(apTenantAllow);
                quantity += i;
            }
        }
        return quantity;
    }

    @Override
    public Map<String, Object> selectEmpower(OperationContext context, int tenantId) {
        EntityWrapper<ApTenantAllow> wrapper = new EntityWrapper<>();
        ApTenantAllow apTenantAllow = new ApTenantAllow();
        apTenantAllow.setTenantID(tenantId);
        apTenantAllow.setCAllowType(Const.OrgBase.AUTHORIZATION_METHOD_L1);
        apTenantAllow.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(apTenantAllow);
        List<ApTenantAllow> apTenantAllowList = apTenantAllowMapper.selectList(wrapper);
        List<ApBase> list = new ArrayList<>();
        if (ToolUtil.isNotEmpty(apTenantAllowList)) {
            for (ApTenantAllow allow : apTenantAllowList) {
                ApBase apBase = new ApBase();
                apBase.setId(allow.getApID());
                ApBase base = ApBaseMapper.selectOne(apBase);
                if (ToolUtil.isNotEmpty(base)) {
                    list.add(base);
                }
            }
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", list);
        return resultMap;
    }

    @Override
    public int moveEmpower(OperationContext context, List<ApBase> apTenantAllowList) {
        int count = 0;
        EntityWrapper<ApTenantAllow> wrapper = new EntityWrapper<>();
        ApTenantAllow apTenantAllow = new ApTenantAllow();
        apTenantAllow.setDelMark(Const.DEL_FLAG_NORMAL);
        apTenantAllow.setTenantID(apTenantAllowList.get(0).getTenantID());
        apTenantAllow.setCAllowType(Const.OrgBase.AUTHORIZATION_METHOD_L1);
        wrapper.setEntity(apTenantAllow);
        List<ApTenantAllow> apTenantAllows = apTenantAllowMapper.selectList(wrapper);
        if (ToolUtil.isNotEmpty(apTenantAllows)) {
            for (ApTenantAllow allow : apTenantAllows) {
                for (ApBase apBase : apTenantAllowList) {
                    if (allow.getApID().equals(apBase.getId())) {
                        allow.setDelMark(Const.DEL_FLAG_DEL);
                        allow.setUpdateUserID(context.getUserId().intValue());
                        Integer integer = apTenantAllowMapper.updateById(allow);
                        count += integer;
                    }
                }
            }
        }
        return count;
    }

    @Override
    public List<ApTenantAllow> getApTenantAllowList(int tenantId) {
        EntityWrapper<ApTenantAllow> wrapper = new EntityWrapper<>();
        ApTenantAllow apTenantAllow = new ApTenantAllow();
        apTenantAllow.setTenantID(tenantId);
        apTenantAllow.setCAllowType(Const.OrgBase.AUTHORIZATION_METHOD_L1);
        apTenantAllow.setDelMark(Const.DEL_FLAG_NORMAL);
        wrapper.setEntity(apTenantAllow);
        List<ApTenantAllow> apTenantAllows = apTenantAllowMapper.selectList(wrapper);
        return apTenantAllows;
    }
}
